#include "gf_types.h"

Vector** allocate_vector_array(unsigned long len1, unsigned long len2){
    Vector **V;
    V = (Vector**)malloc(2*sizeof(Vector));
    V[0] = allocate_vector(len1);
    V[1] = allocate_vector(len2);
    return V;
}

Vector * allocate_vector(unsigned long len) {
    Vector *V;
    V = (Vector *)malloc(sizeof(Vector));
    V->data = (int *)malloc((int)(len * sizeof(int)));
    V->len = (unsigned long)len;
    return V;
}

void free_vector(Vector *V) {
    free(V->data);
    free(V);
}

Vector * copy_vector(Vector *V) {
    Vector *W;
    
    W = allocate_vector(V->len);
    memcpy(W->data, V->data, (V->len)*sizeof(int));

    return W;   
}

int compare_vectors(Vector *V, Vector *W) {
    int equal = 1;
    unsigned long i;

    if (V->len != W->len) return 0;

    for (i = 0; i < V->len; i++) {
        equal = equal && (V->data[i] == W->data[i]);
    }

    return equal; 
}

void print_vector(Vector *V) {
    unsigned long i;
    for (i = 0; i < V->len; i++) {
        printf(" %i",V->data[i]);
    }
    printf("\n");
}

Vector * vector_cat(Vector * U, Vector * V) {
    Vector * W;
    int i, j;

    W = allocate_vector(U->len + V->len);
    for (i = 0; i < U->len; i++) {
        W->data[i] = U->data[i];
    }
    for (j = 0; j < V->len; j++) {
        W->data[i+j] = V->data[j];
    }
    return W;
}

Matrix * allocate_matrix(unsigned long rows, unsigned long cols) {
    Matrix * M;
    unsigned long i;

    M = (Matrix *)malloc(sizeof(Matrix));
    M->rows = rows;
    M->cols = cols;
    M->data = (int **)malloc(rows * sizeof(int *));

    for (i = 0; i < rows; i++) {
        M->data[i] = (int *)malloc(cols * sizeof(int));
    }
    return M;
}

void free_matrix(Matrix *M) {
    unsigned long i;

    for (i = 0; i < M->rows; i++) {
        free(M->data[i]);
    }
    free(M->data);
    free(M);
}

void print_matrix(Matrix *M) {
    unsigned long i, j;

    for (i = 0; i < M->rows; i++) {
        for (j = 0; j < M->cols; j++) {
           printf(" %i",M->data[i][j]);
        }
        printf("\n");    
    }
}
